package com.example.areaofthesquare;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText mEdit;//输入的文本框
    private ImageView mAdd;//添加按钮
    private ImageView mBegin;//开始按钮
    private TextView mShu;//提示文字，“已输入”
    private String mInput = "";//编辑框输入的内容
    private Point mPoint;//点的坐标
    private List<Point> mList = new ArrayList<>();//保存所有点的链表
    private GridView gridView;
    private GridViewAdapter mAdapter;
    private Operation operation;
    private Point pointLeft = new Point();
    private Point pointDown = new Point();
    private Point pointRight = new Point();
    private Point pointUp = new Point();
    private float x;
    private float y;
    private float MaxBian;
    private Point point1 = new Point();
    private Point point2 = new Point();
    private Point point3 = new Point();
    private Point point4 = new Point();
    private List<Point> mSqualeList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView(){

        mShu = findViewById(R.id.shu);

        mAdd = findViewById(R.id.add);
        mAdd.setOnClickListener(this);
        mBegin = findViewById(R.id.begin);
        mBegin.setOnClickListener(this);
        mEdit = findViewById(R.id.edit);
        gridView = findViewById(R.id.grid_view);
        mAdapter = new GridViewAdapter(this,mList);
        gridView.setAdapter(mAdapter);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.add :
                mInput = mEdit.getText().toString();
                if(!mInput.equals("")){
                    mPoint = getPoint(mInput);
                    if(mList.size()==0){
                        pointLeft = mPoint;
                        pointRight = mPoint;
                        pointDown = mPoint;
                        pointUp = mPoint;
                    }else if(mList.size() ==1){
                        pointDown = mPoint;
                    }else{
                        if(mPoint.getX() < pointLeft.getX()){
                            pointLeft = mPoint;
                        }
                        if(mPoint.getX() > pointRight.getY()){
                            pointRight = mPoint;
                        }
                        if(mPoint.getY() < pointDown.getY()){
                            pointDown = mPoint;
                        }
                        if(mPoint.getY() > pointUp.getY()){
                            pointUp = mPoint;
                        }
                    }
                    x = pointRight.getX() - pointLeft.getX();
                    y = pointUp.getY() - pointDown.getY();
                    MaxBian = Math.max(x,y);


                    point1.setX(pointLeft.getX());
                    point1.setY(pointDown.getY());

                    point2.setX(point1.getX());
                    point2.setY(point1.getY() + MaxBian);

                    point3.setX(point1.getX()+MaxBian);
                    point3.setY(point1.getY());

                    point4.setX(point3.getX());
                    point4.setY(point3.getY()+MaxBian);

                    mSqualeList.add(point1);
                    mSqualeList.add(point2);
                    mSqualeList.add(point4);
                    mSqualeList.add(point3);

                    mEdit.setText("");
                    mShu.setVisibility(View.VISIBLE);
                    mList.add(mPoint);
                    mAdapter.notifyDataSetChanged();
                }
                break;
            case R.id.begin :
                if(mList.size() != 0){
                    //跳转到结果展示界面，或者将结果展示到下方
                    // 调用计算函数，界面上出现一个loading的显示。当计算函数计算完成之后，将计算结果返回，
                    // 这里将原本的点的list和计算得到的正方形的面积以及正方形的四个顶点全部传递给结果展示的activity。
//                    operation = new Operation(mList);
//                    double area = operation.getArea();
//                    Log.d("haha", "onClick: " + area);
//                    mSqualeList.clear();
//                    mSqualeList = operation.getFourPoint();
                    Intent intent = new Intent(this,ResultActivity.class);
                    intent.putExtra("pointList", (Serializable) mList);
//                    intent.putExtra("mSqualeList", (Serializable) mSqualeList);
                    startActivity(intent);
                }
                break;

        }

    }

    public Point getPoint(String input){
        Point point = new Point();
        int index = input.indexOf(",");
        float x = Float.valueOf(input.substring(0,index));
        float y = Float.valueOf(input.substring(index+1));
        point.setX(x);
        point.setY(y);
        return point;
    }

}